<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <script src="static/js/jquery.min.js"></script>
     <script src="static/js/weixin.min.js"></script>
    <title></title>
</head>

<body>
    <script type="text/javascript">
    $(function() {
        /*获取url查询字符串值*/
        //注册页面
        var xmUrl = location.href.split('?')[0];
        var succesFunc = function(res) {
        	var xmappId = res.appId;
            var xmtimestamp = res.timestamp;
            var xmnonceStr = res.nonceStr;
            var xmsignature = res.signature;
                        wx.config({
                            debug: true,
                            appId: xmappId,
                            timestamp: xmtimestamp,
                            nonceStr: xmnonceStr,
                            signature: xmsignature,
                            jsApiList: [
                                'checkJsApi',
                                'onMenuShareTimeline',
                                'onMenuShareAppMessage',
                                'onMenuShareQQ',
                                'onMenuShareWeibo',
                                'hideMenuItems',
                                'showMenuItems',
                                'hideAllNonBaseMenuItem',
                                'showAllNonBaseMenuItem',
                                'translateVoice',
                                'startRecord',
                                'stopRecord',
                                'onRecordEnd',
                                'playVoice',
                                'pauseVoice',
                                'stopVoice',
                                'uploadVoice',
                                'downloadVoice',
                                'chooseImage',
                                'previewImage',
                                'uploadImage',
                                'downloadImage',
                                'getNetworkType',
                                'openLocation',
                                'getLocation',
                                'hideOptionMenu',
                                'showOptionMenu',
                                'closeWindow',
                                'scanQRCode',
                                'chooseWXPay',
                                'openProductSpecificView',
                                'addCard',
                                'chooseCard',
                                'openCard'
                            ]
                        });
              wx.error(function(res) {
                    console.log(res);
                });
                var _self = this;
                wx.ready(function() {
                	console.log("注册成功");
               var arg = getUrlValue();
              payAction(arg.orderCode,arg.payType);
                })
             
        }
        var errorFunc = function(res) {

        }
         $.ajax({
        url: "/auth/signature.do?url="+xmUrl,
        type: "get",
        dataType: "json",
        success: succesFunc,
        error: errorFunc
        });
 
        wx.error(function(res) {
           // console.log(res);
        });
    })

    function getUrlValue() {
        var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
            args = {},
            items = qs.length ? qs.split("&") : [],
            item = null,
            name = null,
            value = null,
            i = 0,
            len = items.length;
        for (i = 0; i < len; i++) {
            item = items[i].split("=");
            name = decodeURIComponent(item[0]);
            value = decodeURIComponent(item[1]);
            if (name.length) {
                args[name] = value;
            }
        }
        return args;
    }



    function payAction(orderId,payType) {
        var succesFunc = function(res) {
        	console.log("******");
        	console.log(res);
                     function onBridgeReady() {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest', {
                        "appId": res.appid, //公众号名称，由商户传入     
                        "timeStamp": res.timeStamp, //时间戳，自1970年以来的秒数     
                        "nonceStr": res.nonceStr, //随机串     
                        "package": res.prepay_id,
                        "signType": res.signType, //微信签名方式：     
                        "paySign": res.paySign //微信签名 
                    },
                    function(res) {
                      

                        //
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            var data = {
                                "total": 0,
                                "list": []
                            }
                           // router.replace("/orders/tab2Active");    
                         
                        } // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                        else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                            var data = {
                                "total": 0,
                                "list": []
                            }
                          //router.replace("/orders/tab2Active");  
                           
                        }
                    }
                );
            }
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        };
        var errorFunc = function(res) {
            
        };
        $.ajax({
        url: "/pay/doPay.do?orderCode="+orderId+"&payType="+payType,
        type: "get",
        dataType: "json",
        success: succesFunc,
        error: errorFunc
        });
 
       
    }

    </script>
</body>

</html>
